package com.igeek.javase.ch07.stu.dao;

import com.igeek.javase.ch07.stu.entity.Student;
import com.igeek.javase.ch07.stu.utils.JDBCUtilsByC3P0;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/*
 *@author wangpeng
 *2023/7/20 22:19
 *@description TODO
 */
public class StudentDao extends BaseDao<Student>{
    //1、 统计学生人数
    public int selectCounts() throws SQLException {
        String sql = "select count(*) from stus";
        Long o = (Long)this.selectSingleValue(JDBCUtilsByC3P0.getConn(), sql);
        return o.intValue();
    }
    //2、 查看学生名单  query=""
    //4、 按姓名查询学生信息 query="梅"
    public List<Student> selectAllStus(String query) throws SQLException {
        String sql = "select * from stus where name like concat('%',?,'%')";
        List<Student> students = this.selectAll(JDBCUtilsByC3P0.getConn(), sql, Student.class, query);
        return students;
    }

    //3、 按学号查询学生姓名
    public Student selectOneById(String id) throws SQLException {
        String sql = "select * from stus where id = ?";
        Student student = this.selectOne(JDBCUtilsByC3P0.getConn(), sql, Student.class, id);
        return student;
    }

    //5、 修改学生出生日期
    public int updateStu(String id , Date birthday) throws SQLException {
        String sql = "update stus set birthday = ? where id = ?";
        int i = this.update(JDBCUtilsByC3P0.getConn(), sql, birthday, id);
        return i;
    }

    //6、 删除学生记录
    public int deleteStu(String id) throws SQLException {
        String sql = "delete from stus where id = ?";
        int i = this.update(JDBCUtilsByC3P0.getConn(), sql, id);
        return i;
    }

}
